package cn.tannn.ops.util;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 文件工具
 *
 * @author <a href="https://t.tannn.cn/">tnnn</a>
 * @version V1.0
 * @date 2024/8/30 下午3:56
 */
public class FileUtil {

    /**
     * 压缩文件,多次调用可以将多个文件添加到同一个 zip 文件中
     * @param file  file
     * @param zipOut zipOut
     * @throws IOException IOException
     */
    public static void addToZipFile(File file, ZipOutputStream zipOut) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            ZipEntry zipEntry = new ZipEntry(file.getName());
            zipOut.putNextEntry(zipEntry);
            IOUtils.copy(fis, zipOut);
            zipOut.closeEntry();
        }
    }


    /**
     * 路径最后补齐 /
     * @param path path
     * @return path+/
     */
    public static String pathAppend(String path) {
        String replace = path.replace("\\", "/");
        if(replace.lastIndexOf("/")!=-1){
            replace = path+"/";
        }
        return replace;
    }
}
